package com.yuanlrc.base.controller.admin;

import com.yuanlrc.base.bean.CodeMsg;
import com.yuanlrc.base.bean.PageBean;
import com.yuanlrc.base.bean.Result;
import com.yuanlrc.base.entity.admin.*;
import com.yuanlrc.base.service.admin.*;
import com.yuanlrc.base.util.StringUtil;
import com.yuanlrc.base.util.ValidateEntityUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("/student")
@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private FacultiesService facultiesService;
    @Autowired
    private ClazzService clazzService;

    @Autowired
    private OperaterLogService operaterLogService;

    /**
     * 学生列表信息
     * @param model
     * @param student
     * @param pageBean
     * @return
     */
    @RequestMapping(value="/list")
    public String list(Model model, Student student, PageBean<Student> pageBean){
        model.addAttribute("title", "学生列表");
        model.addAttribute("studentName", student.getStudentName());
        model.addAttribute("pageBean", studentService.findList(student,pageBean));
        return "admin/student/list";
    }

    /**
     * 新增学生页面
     * @param model
     * @return
     */
    @RequestMapping(value="/add",method= RequestMethod.GET)
    public String add(Model model){
        model.addAttribute("role",roleService.findByCode(RoleType.STUDENT.getCode()));
        model.addAttribute("facultiess", facultiesService.findAll());
        return "admin/student/add";
    }
    /**
     * 根据学院查询班级
     * @param id
     * @return
     */
    @RequestMapping(value="/add/faculties",method=RequestMethod.POST)
    @ResponseBody
    public List<Clazz> select(@Param("id") Long id) {
        List<Clazz> clazzes= clazzService.findClazzByFaculties(id);
        return clazzes;
    }

    /**
     * 学生添加表单提交处理
     * @param student
     * @return
     */
    @RequestMapping(value="/add",method=RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> add(Student student){
        //用统一验证实体方法验证是否合法
        CodeMsg validate = ValidateEntityUtil.validate(student);
        if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
            return Result.error(validate);
        }
        if(student.getRole()==null || student.getRole().getId() == null){
            return Result.error(CodeMsg.ADMIN_STUDENT_ROLE_EMPTY);
        }
        if(student.getFaculties() == null || student.getFaculties().getId() == null){
            return Result.error(CodeMsg.ADMIN_STUDENT_FACULTIES_EMPTY);
        }
        if(student.getClazz() == null || student.getClazz().getId() == null){
            return Result.error(CodeMsg.ADMIN_STUDENT_CLAZZ_EMPTY);
        }
        //判断学号是否存在
        if(studentService.isExistStudentId(student.getStudentId(), 0l)){
            return Result.error(CodeMsg.ADMIN_STUDENTID_EXIST);
        }
        //到这说明一切符合条件，进行数据库新增
        if(studentService.save(student) == null){
            return Result.error(CodeMsg.ADMIN_USE_ADD_ERROR);
        }
        operaterLogService.add("添加学生，姓名：" + student.getStudentName());
        return Result.success(true);

    }

    /**
     * 学生编辑页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value="/edit",method=RequestMethod.GET)
    public String edit(Model model,@RequestParam(name="id",required=true)Long id){
        Student student = studentService.find(id);
        Faculties faculties = student.getFaculties();
        Clazz clazz = student.getClazz();
        Long facultiesId = 0L;
        Long clazzId = 0L;

        if(faculties != null && faculties.getId() != null){
            facultiesId = faculties.getId();
        }
        if(clazz != null && clazz.getId() != null){
            clazzId = clazz.getId();
        }

        model.addAttribute("roles",roleService.findAll());
        model.addAttribute("facultiess",facultiesService.findAll());
        model.addAttribute("student", studentService.find(id));
        model.addAttribute("facultiesId",facultiesId);
        model.addAttribute("clazzId",clazzId);

        return "admin/student/edit";
    }

    /**
     * 根据学院查询班级
     * @param id
     * @return
     */
    @RequestMapping(value="/edit/faculties",method=RequestMethod.POST)
    @ResponseBody
    public List<Clazz> selectEdit(@Param("id") Long id) {
        List<Clazz> clazzes= clazzService.findClazzByFaculties(id);
        return clazzes;
    }

    /**
     * 编辑学生信息表单提交处理
     * @param student
     * @return
     */
    @RequestMapping(value="/edit",method=RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> edit(Student student){
        //用统一验证实体方法验证是否合法
        CodeMsg validate = ValidateEntityUtil.validate(student);
        if(validate.getCode() != CodeMsg.SUCCESS.getCode()){
            return Result.error(validate);
        }
        if(student.getRole()==null || student.getRole().getId() == null){
            return Result.error(CodeMsg.ADMIN_STUDENT_ROLE_EMPTY);
        }
        if(student.getFaculties() == null || student.getFaculties().getId() == null){
            return Result.error(CodeMsg.ADMIN_STUDENT_FACULTIES_EMPTY);
        }
        if(student.getClazz() == null || student.getClazz().getId() == null){
            return Result.error(CodeMsg.ADMIN_STUDENT_CLAZZ_EMPTY);
        }
        if(student.getId() == null || student.getId().longValue() <= 0){
            return Result.error(CodeMsg.ADMIN_STUDENT_NO_EXIST);
        }
        if(studentService.isExistStudentId(student.getStudentId(),student.getId())){
            return Result.error(CodeMsg.ADMIN_STUDENTID_EXIST);
        }
        //到这说明一切符合条件，进行数据库保存
        Student findById = studentService.find(student.getId());
        //讲提交的用户信息指定字段复制到已存在的student对象中,该方法会覆盖新字段内容
        BeanUtils.copyProperties(student, findById, "id","createTime","updateTime");
        if(studentService.save(findById) == null){
            return Result.error(CodeMsg.ADMIN_USE_EDIT_ERROR);
        }
        operaterLogService.add("编辑学生，姓名：" + student.getStudentName());
        return Result.success(true);
    }

    /**
     * 删除学生
     * @param id
     * @return
     */
    @RequestMapping(value="/delete",method=RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(name="id",required=true)Long id){
        try {
            studentService.delete(id);
        } catch (Exception e) {
            return Result.error(CodeMsg.ADMIN_USE_DELETE_ERROR);
        }
        operaterLogService.add("删除学生，学生ID：" + id);
        return Result.success(true);
    }


}
